//
//  LZOrderListViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZOrderListViewController.h"
#import "LZCouponOrderTableCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "LZFightGroupDetailViewController.h"
#import "LZGrabCouponDetailViewController.h"
#import "MJRefresh.h"
#import "LZOrderPresenter.h"
#import "LZOrderModel.h"
#import "LZUserModel.h"
#import "UITableView+LZTableView.h"

@interface LZOrderListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *orderTitleCountLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, assign) int currentPage;
@property (nonatomic, assign) int limit;
@property (nonatomic, assign) NSTimeInterval timeStamp;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) LZOrderPresenter *orderPresenter;

@end

@implementation LZOrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.umLogPageViewName = @"订单列表页面";
    
    [self configureUI];
    
    [self configureData];
    
    [self getListDataWithPage:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark private
- (void)upLoadMoreVideoListData
{
    [self getListDataWithPage:self.currentPage];
}

- (void)getListDataWithPage:(int)cPage
{
    WS(wSelf);
    if (self.comeFromFinancialVC) {
        self.title = LZLocalizedString(@"provision_details", nil);
        [self.orderPresenter getFinancialOrderListWithSellerID:[LZUserModel Instance].sellerId PayedState:self.onlyGetNonePayedList ? @"0" : @"1" Page:cPage Limit:self.limit ResultSuccess:^(int totalPage, int totalCount, int currentPage, NSArray *listArr) {
            
            if (totalPage > cPage) {
                [wSelf.tableView.mj_footer endRefreshing];
            }else{
                [wSelf.tableView setNoMoreDataFooterText];
                [wSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            wSelf.orderTitleCountLabel.text = [NSString stringWithFormat:@"%d",totalCount];
            [wSelf.dataArr addObjectsFromArray:listArr];
            wSelf.currentPage ++;
            
            [wSelf.tableView reloadData];
            
        } Failure:^(NSString *fail) {
            
        }];
        
    }else{
        self.title = LZLocalizedString(@"home_menu_manage_orders", nil);
        [self.orderPresenter getOrderListWithSellerID:[LZUserModel Instance].sellerId Page:cPage Limit:self.limit ResultSuccess:^(int totalPage, int totalCount, int currentPage, NSArray *listArr) {
            
            if (totalPage > cPage) {
                [wSelf.tableView.mj_footer endRefreshing];
            }else{
                [wSelf.tableView setNoMoreDataFooterText];
                [wSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            wSelf.orderTitleCountLabel.text = [NSString stringWithFormat:@"%d",totalCount];
            [wSelf.dataArr addObjectsFromArray:listArr];
            wSelf.currentPage ++;
            
            [wSelf.tableView reloadData];
            
        } Failure:^(NSString *fail) {
            DLog(@"%@",fail);
        }];
    }
    
}

#pragma mark -
#pragma mark ui
- (void)configureUI{
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LZCouponOrderTableCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = self.topView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreVideoListData)];
    [self.tableView configFooterRefreshText];
}

- (void)configureData
{
    _currentPage = 1;
    _limit = 10;
    _dataArr = @[].mutableCopy;
    _orderPresenter = [[LZOrderPresenter alloc] init];
}
#pragma mark -
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WS(wSelf);
    return [tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) {
        LZOrderModel *m = wSelf.dataArr[indexPath.row];
        LZCouponOrderTableCell *cellTemp = (LZCouponOrderTableCell *)cell;
        cellTemp.orderModel = m;
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZCouponOrderTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    LZOrderModel *m = self.dataArr[indexPath.row];
    cell.orderModel = m;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LZOrderModel *modelTemp = self.dataArr[indexPath.row];
    if ([modelTemp.promotionType isEqualToString:@"1"]) {//优惠券
        LZGrabCouponDetailViewController *vc = [[LZGrabCouponDetailViewController alloc] init];
        vc.couponId = modelTemp.promotionId;
        [self.navigationController pushViewController:vc animated:YES];

    }else{
        LZFightGroupDetailViewController *vc = [[LZFightGroupDetailViewController alloc] init];
        vc.couponId = modelTemp.promotionId;
        [self.navigationController pushViewController:vc animated:YES];

    }
}

@end
